package leetcode100;

public class lc008 {

    public static void main(String[] args) {
        ListNode head1 = new ListNode(4);
        head1 = new ListNode(2,head1);
        head1 = new ListNode(1,head1);
        head1.visit();

        ListNode head2 = new ListNode(3);
        head2 = new ListNode(2, head2);
        head2 = new ListNode(1, head2);
        head2.visit();

        ListNode head3 = mergeTwoLists(head1,head2);
        head3.visit();

    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null) return list2;
        if(list2==null) return list1;

        ListNode list3 = null;
        ListNode cur1 = list1;
        ListNode cur2 = list2;
        ListNode cur3 = null;
        if(cur1.val<=cur2.val){
            cur3 = list3 = new ListNode(cur1.val);
            cur1 = cur1.next;
        } else {
            cur3 = list3 = new ListNode(cur2.val);
            cur2 = cur2.next;
        }
        while(cur1!=null && cur2!=null){
//            list3.visit();
            /* list1为优先考虑 */
            if(cur1.val<=cur2.val){
                cur3.next = new ListNode(cur1.val);
                cur1 = cur1.next;
                cur3 = cur3.next;
            } else {
                cur3.next = new ListNode(cur2.val);
                cur2 = cur2.next;
                cur3 = cur3.next;
            }
        }
        if(cur1==null){
            cur3.next = cur2;
        } else if(cur2==null){
            cur3.next = cur1;
        }

        return list3;
    }
}
